#!/bin/bash

export LANGUAGE=en_US
export LANG=en_US.UTF-8

TMPFILE="/tmp/cepreinfo_tmp_$$"
WRAPFILE="/tmp/cepreinfo"

BASEDIR=$(cd "$(dirname "$0")"; pwd)

if [ $# == 6 ]
then
    echo '#!/usr/bin/expect -f' > $WRAPFILE
    echo 'set ip [lindex $argv 0]' >> $WRAPFILE
    echo 'set user [lindex $argv 1]' >> $WRAPFILE
    echo 'set passwd [lindex $argv 2]' >> $WRAPFILE
    echo "spawn scp $0 \$user@\$ip:/tmp/rcepreinfo" >> $WRAPFILE
    echo 'expect "password: "' >> $WRAPFILE
    echo 'send "$passwd\r"' >> $WRAPFILE
    echo 'expect eof' >> $WRAPFILE
    echo 'spawn ssh $user@$ip "/tmp/rcepreinfo > /tmp/rinfo.log"' >> $WRAPFILE
    echo 'expect "password: "' >> $WRAPFILE
    echo 'send "$passwd\r"' >> $WRAPFILE
    echo 'expect eof' >> $WRAPFILE
    echo "spawn scp \$user@\$ip:/tmp/rinfo.log  \"$4\"" >> $WRAPFILE
    echo 'expect "password: "' >> $WRAPFILE
    echo 'send "$passwd\r"' >> $WRAPFILE
    echo 'expect eof' >> $WRAPFILE

    chmod +x $WRAPFILE
    $WRAPFILE "$1" "$2" "$3"
    echo "Description=$5" >> "$4"
    echo "machrole=$6" >> "$4"
    exit 0
fi

echo "kernelname=`uname -rm`"
echo "osrelease=`cat /etc/*release | grep \"DISTRIB_DESCRIPTION\|PRETTY_NAME\" | awk -F '=' '{print $2}' | xargs`"
echo "brandmodel=`dmidecode 2>/dev/null | head -n 20 | grep "Vendor\|Version" | awk -F ':' '{print $2}' | xargs`"

echo "cpumodel=`lscpu | grep "Model name" | awk -F ':' '{print $2}' | xargs`"
csocks=`lscpu | grep "Socket(s)" | awk -F ':' '{print $2}' | xargs`
echo "cpusocks=$csocks"
ccores=`lscpu | grep "Core(s)" | awk -F ':' '{print $2}' | xargs`
ccores=$(($csocks * $ccores))
cthrs=`lscpu | grep "Thread(s)" | awk -F ':' '{print $2}' | xargs`
cthrs=$(($cthrs * $ccores))
echo "cpucores=$ccores"
echo "cputhrs=$cthrs"
echo "numacount=`lscpu | grep "NUMA" | awk -F ':' '{print $2}' | xargs`"

mtotal=`cat /proc/meminfo  | grep MemTotal | awk -F ':' '{print $2}' | xargs`
echo "memtotal=$mtotal, `dmidecode 2>/dev/null | grep -A16 "Memory Device$" | grep "Size" | grep -v "No Module" | awk -F ':' '{print $2\" |\"}' | xargs | sed \"s/|$//\"`"
echo "memcount=`dmidecode 2>/dev/null | grep -A16 "Memory Device$" | grep "Size" | grep -v "No Module" | wc -l`"
echo "memmanu=`dmidecode 2>/dev/null | grep -A16 "Memory Device$" | grep "Manufacturer" | grep -v "Not Specified" | grep -v "NO DIMM" | awk -F ':' '{print $2" |"}' | xargs | sed \"s/|$//\"`"
echo "memtype=`dmidecode 2> /dev/null | grep -A16 "Memory Device$" | grep "Type:" | grep -v "Unknown" | awk -F ':' '{print $2" |"}' | xargs | sed \"s/|$//\"`"
echo "memspeed=`dmidecode 2> /dev/null | grep -A16 "Memory Device$" | grep "Speed:" | grep -v "Unknown" | awk -F ':' '{print $2" |"}' | xargs | sed \"s/|$//\"`"

disks=`fdisk -l | grep -v "dev/ram" | grep "^Disk /dev/sd" | awk -F ':' '{print $1}' | awk -F '/' '{print $3}'`
rm -f $TMPFILE
touch $TMPFILE
for disk in ${disks[@]}
do
    size=`fdisk -l | grep -v "dev/ram" | grep "^Disk /dev/sd" | grep "sda:" | awk '{print $3 $4}'`
    dtype=`ls -oQ /dev/disk/by-id/ | grep -v "part" | grep "\<$disk\"" | grep -v "\-0x" | awk -F '"' '{print $2}' | awk -F '-' '{print $1"-"$2}'`
    echo "$size$dtype" >> $TMPFILE
done
cat $TMPFILE | sort -n | uniq -c | sed "s/^.[ ]*//" | sed "s/ /\*/" | awk '{print "harddisk="$1}'

echo "raidinfo=unknown"

nets=`lspci | grep "Ethernet" | sed "s/ Ethernet controller//" | sed -e "s/ PCI .*//"  -e "s/ Network Controller.*//" | sed -e "s/: /|/" -e "s/ /!/g"`
netds=`/sbin/ifconfig | grep flags | grep "^e" | awk '{print $1}' | awk -F ':' '{print $1}'`
rm -f $TMPFILE
touch $TMPFILE
for netd in ${netds[@]}
do
    dpci=`find /sys/devices | grep $netd\$`
    info=" "
    for net in ${nets[@]}
    do
        pciid=`echo $net | awk -F '|' '{print $1}' | sed "s/:$//"`
        if [[ $dpci =~ ":$pciid/net" ]]
        then
            info=`echo "$net" | awk -F '|' '{print $2}' | sed "s/!/ /g"`
            break
        fi
    done
    speed=`ethtool $netd | grep "/Full\>" | sed "s/.*Half//" | tail -n 1 | sed "s/.*://" | xargs`
    echo "$speed $info" >> $TMPFILE
done
cat $TMPFILE | sort -n | uniq -c | sed "s/^.[ ]*//" | sed "s/ /\*/" | awk -F '|' '{print "ethcard="$1}'


exit 0
